package cn.su.servlet;

import cn.su.pojo.provider;
import cn.su.service.billlistService;
import cn.su.service.impl.billlistServiceimpl;
import cn.su.service.impl.providerServiceimpl;
import cn.su.service.providerService;
import com.sun.corba.se.spi.orbutil.fsm.FSMImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

import java.text.ParseException;
import java.text.SimpleDateFormat;


import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;

@WebServlet(name = "providerServlet", value = "/jsp/provider.do")
public class providerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");

        HttpSession session = request.getSession();
        String method = request.getParameter("method");//用来判断执行什么功能
        providerService providerService=new providerServiceimpl();//用来执行Dao
   if (method.equals("query")){

       List<provider> providers = providerService.provider_List();
       request.setAttribute("providerList",providers);
       //如果查詢存在参数，则查询 注意这里不会为null，queryProCode=""
       if (request.getParameter("queryProCode")!=null&&request.getParameter("queryProName")!=null){
           String code=request.getParameter("queryProCode");
           String name=request.getParameter("queryProName");
           request.setAttribute("queryProCode",code);
           request.setAttribute("queryProName",name);
           List<provider>  provider = providerService.provider_search(code, name);
           request.setAttribute("providerList",provider);

       }
       request.getRequestDispatcher("providerlist.jsp").forward(request,response);
   }

        if (method.equals("add")) {
            String proCode = request.getParameter("proCode");
            String proName = request.getParameter("proName");
            String proContact = request.getParameter("proContact");
            String proPhone = request.getParameter("proPhone");
            String proAddress = request.getParameter("proAddress");
            String proFax = request.getParameter("proFax");
            String proDesc = request.getParameter("proDesc");
            provider provider = new provider();
            provider.setProCode(proCode);
            provider.setProName(proName);
            provider.setProContact(proContact);
            provider.setProPhone(proPhone);
            provider.setProAddress(proAddress);
            provider.setProFax(proFax);
            provider.setProDesc(proDesc);
            provider.setCreationDate(new Date());//创建时间
            int i = providerService.provider_add(provider);
            if (i >= 1) {
                request.setAttribute("queryProCode", proCode);
                request.setAttribute("queryProName",proName);
                List<provider> providers = providerService.provider_search(proCode, proName);
                request.setAttribute("providerList",providers);
                System.out.println(providers+"===");
                request.getRequestDispatcher("providerlist.jsp").forward(request, response);
            } else {
                request.getRequestDispatcher("error.jsp").forward(request, response);
            }
        }

          if (method.equals("chakan")){
              String id1 = request.getParameter("id");
              int id=Integer.parseInt(id1);
              provider provider = providerService.provider_chakan(id);
              request.setAttribute("provider",provider);
             request.getRequestDispatcher("providerview.jsp").forward(request,response);
          }
          if (method.equals("xiugai")){
              String id1 = request.getParameter("id");
              int id=Integer.parseInt(id1);
              provider provider = providerService.provider_chakan(id);
              request.setAttribute("provider",provider);
              request.getRequestDispatcher("providermodify.jsp").forward(request,response);
          }
          if (method.equals("xiugaisave")){
              String id1 = request.getParameter("id");
              int id=Integer.parseInt(id1);
              String proCode = request.getParameter("proCode");
              String proName = request.getParameter("proName");
              String proContact = request.getParameter("proContact");
              String proPhone = request.getParameter("proPhone");
              String proAddress = request.getParameter("proAddress");
              String proFax = request.getParameter("proFax");
              String proDesc = request.getParameter("proDesc");
              provider provider = new provider();
              provider.setProCode(proCode);
              provider.setProName(proName);
              provider.setProContact(proContact);
              provider.setProPhone(proPhone);
              provider.setProAddress(proAddress);
              provider.setProFax(proFax);
              provider.setProDesc(proDesc);
              //这里要格式化时间

              provider.setModifyDate(new Date());//创建时间

              int i = providerService.provider_update(id, provider);
              if (i>0){
                  List<cn.su.pojo.provider> providers = providerService.provider_search(proCode, proName);
                  request.setAttribute("providerList",providers);
                  request.getRequestDispatcher("providerlist.jsp").forward(request, response);
              }else{
                  request.getRequestDispatcher("error.jsp").forward(request, response);
              }
          }
         if (method.equals("delete")){
             String id1 = request.getParameter("id");
             int id=Integer.parseInt(id1);
             int i = providerService.provider_delete(id);
             if (i>0){
                 response.getWriter().write("<script type='text/javascript'>alert('删除成功')</script>");
                 request.getRequestDispatcher("provider.do?method=query").forward(request, response);
             }else{
                 request.getRequestDispatcher("error.jsp").forward(request, response);
             }
         }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
